using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using DTO;

namespace DAL
{
  public class ThongTinDuLich_DAL
    {
        private SqlConnection cn = null;
        public  ThongTinDuLich_DAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["StrConnection"].ConnectionString);
            cn.Open();
        }
      public void Insert(ThongTinDuLich_DTO ThongTinDuLich)
        {
            SqlCommand cm = new SqlCommand("ThongTinDuLich_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaTuyen",ThongTinDuLich .MaTuyen     );
            cm.Parameters.AddWithValue("@TenTuyen",ThongTinDuLich .TenTuyen);
            cm.Parameters.AddWithValue("@GiaTuyen", ThongTinDuLich.GiaTuyen);
            cm.Parameters.AddWithValue("@GiaGiam", ThongTinDuLich.GiaGiam);
            cm.Parameters.AddWithValue("@ThoiGian_BatDau", ThongTinDuLich.ThoiGian_BatDau);
            cm.Parameters.AddWithValue("@ThoiGian_KetThuc", ThongTinDuLich.ThoiGian_KetThuc );
            cm.Parameters.AddWithValue("@SoLuongVeBan", ThongTinDuLich.SoLuongVeBan );
            cm.Parameters.AddWithValue("@SoLuongVeConLai", ThongTinDuLich.SoLuongVeConLai );
            cm.Parameters.AddWithValue("@GiaGiam", ThongTinDuLich.MaTinhTrang_SP);
            cm.Parameters.AddWithValue("@MaTinhTrang_SP", ThongTinDuLich.PhiGiaoHangTanNoi);
            cm.Parameters.AddWithValue("@PhiGiaoHangTanNoi", ThongTinDuLich.Hinh);
            cm.Parameters.AddWithValue("@Hinh", ThongTinDuLich.MoTa);
                            
            cm.ExecuteNonQuery();
        }
      public void Update(ThongTinDuLich_DTO ThongTinDuLich)
        {
            SqlCommand cm = new SqlCommand("ThongTinDuLich_Upsate", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaTuyen", ThongTinDuLich.MaTuyen);
            cm.Parameters.AddWithValue("@TenTuyen", ThongTinDuLich.TenTuyen);
            cm.Parameters.AddWithValue("@GiaTuyen", ThongTinDuLich.GiaTuyen);
            cm.Parameters.AddWithValue("@GiaGiam", ThongTinDuLich.GiaGiam);
            cm.Parameters.AddWithValue("@ThoiGian_BatDau", ThongTinDuLich.ThoiGian_BatDau);
            cm.Parameters.AddWithValue("@ThoiGian_KetThuc", ThongTinDuLich.ThoiGian_KetThuc);
            cm.Parameters.AddWithValue("@SoLuongVeBan", ThongTinDuLich.SoLuongVeBan);
            cm.Parameters.AddWithValue("@SoLuongVeConLai", ThongTinDuLich.SoLuongVeConLai);
            cm.Parameters.AddWithValue("@GiaGiam", ThongTinDuLich.MaTinhTrang_SP);
            cm.Parameters.AddWithValue("@MaTinhTrang_SP", ThongTinDuLich.PhiGiaoHangTanNoi);
            cm.Parameters.AddWithValue("@PhiGiaoHangTanNoi", ThongTinDuLich.Hinh);
            cm.Parameters.AddWithValue("@Hinh", ThongTinDuLich.MoTa);

            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("ThongTinDuLich_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaTuyen", iD);
            cm.ExecuteNonQuery();
        }
      public ThongTinDuLichCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("ThongTinDuLich_GET", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            ThongTinDuLichCollection ThongTinDuLichCollection = new ThongTinDuLichCollection();

            while (dr.Read ())
            {
                ThongTinDuLich_DTO ThongTinDuLich = new ThongTinDuLich_DTO();
                ThongTinDuLich.MaTuyen = int.Parse(dr.GetValue(0).ToString());
                ThongTinDuLich.TenTuyen = dr.GetValue(1).ToString();
                ThongTinDuLich.GiaTuyen =float .Parse ( dr.GetValue(2).ToString());
                ThongTinDuLich.GiaGiam= float .Parse (dr.GetValue(3).ToString());
                ThongTinDuLich.ThoiGian_BatDau = DateTime.Parse(dr.GetValue(4).ToString());
                ThongTinDuLich.ThoiGian_KetThuc = DateTime .Parse(dr.GetValue(5).ToString());
                ThongTinDuLich.SoLuongVeBan  =int.Parse ( dr.GetValue(6).ToString());
                ThongTinDuLich.SoLuongVeConLai =int.Parse  (dr.GetValue(7).ToString());
                ThongTinDuLich.MaTinhTrang_SP =int.Parse ( dr.GetValue(8).ToString());
                ThongTinDuLich.PhiGiaoHangTanNoi =int.Parse(dr.GetValue(9).ToString());
                ThongTinDuLich.Hinh = dr.GetValue(10).ToString();
                ThongTinDuLich.MoTa = dr.GetValue(11).ToString();

                ThongTinDuLichCollection.Add(ThongTinDuLich);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return ThongTinDuLichCollection  ;
        }
    }
}
